package com.guyu.plugin.mybatis.entity;

import com.guyu.common.core.enums.YesOrNoEnum;
import com.mybatisflex.annotation.Column;
import lombok.Data;

import java.time.LocalDateTime;
import java.util.Objects;

@Data
public class BaseDO {
    /**
     * 创建者
     */
    protected Long createBy;
    /**
     * 创建时间
     */
    @Column(onInsertValue = "now()")
    protected LocalDateTime createTime;
    /**
     * 更新者
     */
    protected Long updateBy;
    /**
     * 更新时间
     */
    @Column(onInsertValue = "now()", onUpdateValue = "now()")
    protected LocalDateTime updateTime;
    /**
     * 删除字段
     *
     * @see YesOrNoEnum
     */
    @Column(isLogicDelete = true)
    protected Integer deleted;

    /**
     * 初始化
     */
    public void init() {
        init(LocalDateTime.now());
    }

    /**
     * 初始化
     *
     * @param date 当前时间
     */
    public void init(LocalDateTime date) {
        this.deleted = YesOrNoEnum.NO.getCode();
        this.createTime = date;
        this.updateTime = date;
    }

    /**
     * BO转DO
     */
    public void from(BaseBO baseBO) {
        if (Objects.isNull(baseBO)) {
            return;
        }
        this.createBy = baseBO.getCreateBy();
        this.createTime = baseBO.getCreateTime();
        this.updateBy = baseBO.getUpdateBy();
        this.updateTime = baseBO.getUpdateTime();
        this.deleted = YesOrNoEnum.getCode(baseBO.isDeleted());
    }

    /**
     * 是否删除
     */
    public Boolean hasDeleted() {
        return YesOrNoEnum.isYes(this.deleted);
    }

}